package com.cskaoyan.service;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.bean.CouponExample;
import com.cskaoyan.bean.CouponUser;
import com.cskaoyan.bean.CouponUserExample;
import com.cskaoyan.bean.vo.AdminAdForListDataBean;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class AdminCouponServiceImp implements AdminCouponService {

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CouponUserMapper couponUserMapper;

    @Override
    public AdminAdForListDataBean<Coupon> queryCoupon(Integer page, Integer limit, String name, String sort, String order, Short type, Short status) {
        PageHelper.startPage(page, limit);

        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();

        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andGoodsTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);
        couponExample.setOrderByClause(sort + " " + order);
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        long total = couponPageInfo.getTotal();
        AdminAdForListDataBean<Coupon> couponAdminAdForListDataBean = new AdminAdForListDataBean<>(total, coupons);
        return couponAdminAdForListDataBean;
    }

    @Override
    public Coupon queryCouponById(Integer id) {
        return couponMapper.selectByPrimaryKey(id);
    }

    @Override
    public AdminAdForListDataBean<CouponUser> queryCouponUsers(Integer page, Integer limit, Integer couponId, Integer userId, Short status, String sort, String order) {
        PageHelper.startPage(page, limit);
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        if (couponId !=  null) {
            criteria.andCouponIdEqualTo(couponId);
        }
        criteria.andDeletedEqualTo(false);
        couponUserExample.setOrderByClause(sort + " " + order);
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        PageInfo<CouponUser> couponUserPageInfo = new PageInfo<>(couponUsers);
        AdminAdForListDataBean<CouponUser> couponUserAdminAdForListDataBean = new AdminAdForListDataBean<>(couponUserPageInfo.getTotal(), couponUsers);
        return couponUserAdminAdForListDataBean;
    }

    @Override
    public Integer AddCoupon(Coupon coupon) {

        coupon.setAddTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        coupon.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        int result = couponMapper.insertSelective(coupon);
        return result;
    }

    @Override
    public Integer deleteCoupon(Coupon coupon) {
        coupon.setDeleted(true);
        int result = couponMapper.updateByPrimaryKeySelective(coupon);
        return result;
    }

    @Override
    public Integer updateCoupon(Coupon coupon) {
        int result = couponMapper.updateByPrimaryKeySelective(coupon);
        return result;
    }
}
